package org.flowers.unbeaten.generator.configuration;

import org.flowers.unbeaten.generator.constant.SystemConstant;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @description Web配置
 * @author RyanWang
 * @date 2021-08-13 14:39:27
 * @version 1.0
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        // 自定义Long类型转换
        builder.serializerByType(Long.class, new CustomLongConverter());
        builder.serializerByType(Long.TYPE, new CustomLongConverter());
        // 时间类型格式转换,默认为long
        builder.indentOutput(true).dateFormat(new SimpleDateFormat(SystemConstant.DATE_FORMAT));
        converters.add(0, new MappingJackson2HttpMessageConverter(builder.build()));
    }

}
